<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>DigraphGenerator.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="DigraphGenerator code in Java">
<meta NAME="TITLE" CONTENT="DigraphGenerator code in Java">
<meta NAME="KEYWORDS" CONTENT="DigraphGenerator,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>DigraphGenerator.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "DigraphGenerator.java">DigraphGenerator.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac DigraphGenerator.java</span>
<span class="comment"> *  Execution:    java DigraphGenerator V E</span>
<span class="comment"> *  Dependencies: Digraph.java</span>
<span class="comment"> *</span>
<span class="comment"> *  A digraph generator.</span>
<span class="comment"> *  </span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">DigraphGenerator</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class provides static methods for creating</span>
<span class="comment"> *  various digraphs, including Erdos-Renyi random digraphs, random DAGs,</span>
<span class="comment"> *  random rooted trees, random rooted DAGs, random tournaments, path digraphs,</span>
<span class="comment"> *  cycle digraphs, and the complete digraph.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/42digraph"</span><span class="keyword">&gt;</span><span class="comment">Section 4.2</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">DigraphGenerator</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Edge</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> Comparable</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> w</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">v </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">w </span><span class="symbol">=</span><span class="normal"> w</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compareTo</span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">v </span><span class="symbol">&lt;</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">v</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">v </span><span class="symbol">&gt;</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">v</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">w </span><span class="symbol">&lt;</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">w</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">w </span><span class="symbol">&gt;</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">w</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// this class cannot be instantiated</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">DigraphGenerator</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random simple digraph containing </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> E the number of vertices</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random simple digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices, containing a total</span>
<span class="comment">     *     of </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if no such simple digraph exists</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">simple</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="symbol">(</span><span class="type">long</span><span class="symbol">)</span><span class="normal"> V</span><span class="symbol">*(</span><span class="normal">V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Too many edges"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal">              </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Too few edges"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">SET&lt;Edge&gt;</span><span class="normal"> set </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> SET</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">E</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">v </span><span class="symbol">!=</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">set</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                set</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns a random simple digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices, with an </span>
<span class="comment">     * edge between any two vertices with probability </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">. This is sometimes</span>
<span class="comment">     * referred to as the Erdos-Renyi random digraph model.</span>
<span class="comment">     * This implementations takes time propotional to V^2 (even if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is small).</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> p the probability of choosing an edge</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random simple digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices, with an edge between</span>
<span class="comment">     *     any two vertices with probability </span><span class="keyword">&lt;tt&gt;</span><span class="comment">p</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if probability is not between 0 and 1</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">simple</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> p</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">p </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0.0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> p </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Probability must be between 0 and 1"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> w </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> w</span><span class="symbol">++)</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">!=</span><span class="normal"> w</span><span class="symbol">)</span>
<span class="normal">                    </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">StdRandom</span><span class="symbol">.</span><span class="function">bernoulli</span><span class="symbol">(</span><span class="normal">p</span><span class="symbol">))</span>
<span class="normal">                        G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the complete digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the complete digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">complete</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">simple</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> V</span><span class="symbol">*(</span><span class="normal">V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random simple DAG containing </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges.</span>
<span class="comment">     * Note: it is not uniformly selected at random among all such DAGs.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> E the number of vertices</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random simple DAG on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices, containing a total</span>
<span class="comment">     *     of </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if no such simple DAG exists</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">dag</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="symbol">(</span><span class="type">long</span><span class="symbol">)</span><span class="normal"> V</span><span class="symbol">*(</span><span class="normal">V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> </span><span class="number">2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Too many edges"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal">                  </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Too few edges"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">SET&lt;Edge&gt;</span><span class="normal"> set </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> SET</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vertices </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">V</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">        StdRandom</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">E</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">v </span><span class="symbol">&lt;</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">set</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                set</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// tournament</span>
<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random tournament digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices. A tournament digraph</span>
<span class="comment">     * is a DAG in which for every two vertices, there is one directed edge.</span>
<span class="comment">     * A tournament is an oriented complete graph.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random tournament digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">tournament</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span><span class="normal"> w </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> w</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">StdRandom</span><span class="symbol">.</span><span class="function">bernoulli</span><span class="symbol">(</span><span class="number">0.5</span><span class="symbol">))</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">                          G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random rooted-in DAG on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges.</span>
<span class="comment">     * A rooted in-tree is a DAG in which there is a single vertex</span>
<span class="comment">     * reachable from every other vertex.</span>
<span class="comment">     * The DAG returned is not chosen uniformly at random among all such DAGs.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> E the number of edges</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random rooted-in DAG on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">rootedInDAG</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="symbol">(</span><span class="type">long</span><span class="symbol">)</span><span class="normal"> V</span><span class="symbol">*(</span><span class="normal">V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> </span><span class="number">2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Too many edges"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">)</span><span class="normal">                </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Too few edges"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">SET&lt;Edge&gt;</span><span class="normal"> set </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> SET</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;();</span>

<span class="normal">        </span><span class="comment">// fix a topological order</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vertices </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">V</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">        StdRandom</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// one edge pointing from each vertex, other than the root = vertices[V-1]</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">            set</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">            G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">E</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">v </span><span class="symbol">&lt;</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">set</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                set</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random rooted-out DAG on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges.</span>
<span class="comment">     * A rooted out-tree is a DAG in which every vertex is reachable from a</span>
<span class="comment">     * single vertex.</span>
<span class="comment">     * The DAG returned is not chosen uniformly at random among all such DAGs.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> E the number of edges</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random rooted-out DAG on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">rootedOutDAG</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="symbol">(</span><span class="type">long</span><span class="symbol">)</span><span class="normal"> V</span><span class="symbol">*(</span><span class="normal">V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> </span><span class="number">2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Too many edges"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">)</span><span class="normal">                </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Too few edges"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">SET&lt;Edge&gt;</span><span class="normal"> set </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> SET</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;();</span>

<span class="normal">        </span><span class="comment">// fix a topological order</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vertices </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">V</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">        StdRandom</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// one edge pointing from each vertex, other than the root = vertices[V-1]</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">            set</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">            G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">E</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">v </span><span class="symbol">&lt;</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">set</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                set</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random rooted-in tree on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices.</span>
<span class="comment">     * A rooted in-tree is an oriented tree in which there is a single vertex</span>
<span class="comment">     * reachable from every other vertex.</span>
<span class="comment">     * The tree returned is not chosen uniformly at random among all such trees.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random rooted-in tree on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">rootedInTree</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">rootedInDAG</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a random rooted-out tree on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices. A rooted out-tree</span>
<span class="comment">     * is an oriented tree in which each vertex is reachable from a single vertex.</span>
<span class="comment">     * It is also known as a </span><span class="keyword">&lt;em&gt;</span><span class="comment">arborescence</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> or </span><span class="keyword">&lt;em&gt;</span><span class="comment">branching</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment">     * The tree returned is not chosen uniformly at random among all such trees.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random rooted-out tree on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">rootedOutTree</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">rootedOutDAG</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a path digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices in the path</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a digraph that is a directed path on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">path</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vertices </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">V</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">        StdRandom</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a complete binary tree digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices in the binary tree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a digraph that is a complete binary tree on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">binaryTree</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vertices </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">V</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">        StdRandom</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[(</span><span class="normal">i</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">)/</span><span class="number">2</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a cycle digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices in the cycle</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a digraph that is a directed cycle on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">cycle</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vertices </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">V</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> i</span><span class="symbol">;</span>
<span class="normal">        StdRandom</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns an Eulerian cycle digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  V the number of vertices in the cycle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  E the number of edges in the cycle</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a digraph that is a directed Eulerian cycle on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices</span>
<span class="comment">     *         and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either V </span><span class="preproc">&amp;le;</span><span class="comment"> 0 or E </span><span class="preproc">&amp;le;</span><span class="comment"> 0</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">eulerianCycle</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"An Eulerian cycle must have at least one edge"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">V </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"An Eulerian cycle must have at least one vertex"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vertices </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">E</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> E</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> E</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">E</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns an Eulerian path digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  V the number of vertices in the path</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  E the number of edges in the path</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a digraph that is a directed Eulerian path on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices</span>
<span class="comment">     *         and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either V </span><span class="preproc">&amp;le;</span><span class="comment"> 0 or E </span><span class="preproc">&amp;lt;</span><span class="comment"> 0</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">eulerianPath</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"negative number of edges"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">V </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"An Eulerian path must have at least one vertex"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vertices </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">E</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> E</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> E</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns a random simple digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * edges and (at least) </span><span class="keyword">&lt;tt&gt;</span><span class="comment">c</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> strong components. The vertices are randomly</span>
<span class="comment">     * assigned integer labels between </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">c-1</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> (corresponding to </span>
<span class="comment">     * strong components). Then, a strong component is creates among the vertices</span>
<span class="comment">     * with the same label. Next, random edges (either between two vertices with</span>
<span class="comment">     * the same labels or from a vetex with a smaller label to a vertex with a </span>
<span class="comment">     * larger label). The number of components will be equal to the number of</span>
<span class="comment">     * distinct labels that are assigned to vertices.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> V the number of vertices</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> E the number of edges</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> c the (maximum) number of strong components</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a random simple digraph on </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> vertices and</span>
<span class="comment">               </span><span class="keyword">&lt;tt&gt;</span><span class="comment">E</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> edges, with (at most) </span><span class="keyword">&lt;tt&gt;</span><span class="comment">c</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> strong components</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">c</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is larger than </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> </span><span class="function">strong</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> V</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> E</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> c</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">c </span><span class="symbol">&gt;=</span><span class="normal"> V </span><span class="symbol">||</span><span class="normal"> c </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Number of components must be between 1 and V"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">2</span><span class="symbol">*(</span><span class="normal">V</span><span class="symbol">-</span><span class="normal">c</span><span class="symbol">))</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Number of edges must be at least 2(V-c)"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">E </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="symbol">(</span><span class="type">long</span><span class="symbol">)</span><span class="normal"> V</span><span class="symbol">*(</span><span class="normal">V</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> </span><span class="number">2</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Too many edges"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// the digraph</span>
<span class="normal">        </span><span class="usertype">Digraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// edges added to G (to avoid duplicate edges)</span>
<span class="normal">        </span><span class="usertype">SET&lt;Edge&gt;</span><span class="normal"> set </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> SET</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;();</span>

<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> label </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">V</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            label</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">c</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// make all vertices with label c a strong component by</span>
<span class="normal">        </span><span class="comment">// combining a rooted in-tree and a rooted out-tree</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> c</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="comment">// how many vertices in component c</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> count </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">label</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> count</span><span class="symbol">++;</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="comment">// if (count == 0) System.err.println("less than desired number of strong components");</span>

<span class="normal">            </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> vertices </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">count</span><span class="symbol">];</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> V</span><span class="symbol">;</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">label</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">++]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            StdRandom</span><span class="symbol">.</span><span class="function">shuffle</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">);</span>

<span class="normal">            </span><span class="comment">// rooted-in tree with root = vertices[count-1]</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> count</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> count</span><span class="symbol">);</span>
<span class="normal">                </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">                set</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="comment">// rooted-out tree with root = vertices[count-1]</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> count</span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> count</span><span class="symbol">);</span>
<span class="normal">                </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">                set</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">vertices</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">],</span><span class="normal"> vertices</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">E</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> E</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">set</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> v </span><span class="symbol">!=</span><span class="normal"> w </span><span class="symbol">&amp;&amp;</span><span class="normal"> label</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&lt;=</span><span class="normal"> label</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                set</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> G</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">DigraphGenerator</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> library.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> V </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> E </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"complete graph"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">complete</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"simple"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">simple</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> E</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"path"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">path</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"cycle"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">cycle</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Eulierian path"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">eulerianPath</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> E</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Eulierian cycle"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">eulerianCycle</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> E</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"binary tree"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">binaryTree</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"tournament"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">tournament</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"DAG"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">dag</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> E</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"rooted-in DAG"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">rootedInDAG</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> E</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"rooted-out DAG"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">rootedOutDAG</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> E</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"rooted-in tree"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">rootedInTree</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"rooted-out DAG"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="function">rootedOutTree</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
